Καλώς ορίσατε στο dotNETZone.gr - Σύνδεση | Εγγραφή | Βοήθεια
σε

 

Αρχική σελίδα Ιστολόγια Συζητήσεις Εκθέσεις Φωτογραφιών Αρχειοθήκες

Threading Examble

Îåêßíçóå áðü ôï ìÝëïò imanos. Τελευταία δημοσίευση από το μέλος imanos στις 30-01-2006, 15:06. Υπάρχουν 0 απαντήσεις.
Ταξινόμηση Δημοσιεύσεων: Προηγούμενο Επόμενο
  •  30-01-2006, 15:06 9027

    Threading Examble

    Έτσι κουβέντα να γίνεται.

    Ένα απλό παράδειγμα για threading.

    Imports System

    Imports System.Drawing

    Imports System.Collections

    Imports System.ComponentModel

    Imports System.Threading

    #Region WINDOWS GENERATE CODE

    Public Class Form1

    Inherits System.Windows.Forms.Form

    Private Thread1 As Thread

    Private Thread2 As Thread

    Private Thread3 As Thread

    Dim i, s, f As Integer

    Sub TheThread1()

    Try

    ProgressBar1.Minimum = 0

    ProgressBar1.Maximum = 100000

    For i = 0 To 100000 Step 10

    Label1.Text = "Loading Record " + i.ToString() + " of 100000"

    ProgressBar1.Value = i

    Thread1.Sleep(1)

    Next

    Catch exc As Exception

    MessageBox.Show(exc.ToString)

    End Try

    End Sub

    Sub TheThread2()

    Try

    ProgressBar2.Minimum = 0

    ProgressBar2.Maximum = 100000

    For s = 0 To 100000 Step 10

     

    Label2.Text = "Loading Record " + s.ToString() + " of 100000"

    ProgressBar2.Value = s

    Thread2.Sleep(1)

    Next

    Catch exc As Exception

    MessageBox.Show(exc.ToString)

    End Try

    End Sub

    Sub TheThread3()

    Try

    ProgressBar3.Minimum = 0

    ProgressBar3.Maximum = 100000

    For f = 0 To 100000 Step 10

    Label3.Text = "Loading Record " + f.ToString() + " of 100000"

    ProgressBar3.Value = f

    Thread3.Sleep(1)

    Next

    Catch exc As Exception

    MessageBox.Show(exc.ToString)

    End Try

    End Sub

    Private Sub BtnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnStart.Click

    BtnPause.Enabled = True 'Can now press the pause button.

    BtnStart.Enabled = False 'Can't press start button again.

    'Declare threads as new threads.

    'Notice the Thread.New function only takes AddressOf, this is the address of the Sub basically.)

    Thread1 = New Threading.Thread(AddressOf TheThread1)

    Thread2 = New Threading.Thread(AddressOf TheThread2)

    Thread3 = New Threading.Thread(AddressOf TheThread3)

    If PrioritiesCheckbox.Checked = True Then

    'Make their priorities different.

    Thread1.Priority = Threading.ThreadPriority.Highest

    Thread2.Priority = Threading.ThreadPriority.Normal

    Thread3.Priority = Threading.ThreadPriority.Lowest

    Else

    Thread1.Priority = Threading.ThreadPriority.Normal

    Thread2.Priority = Threading.ThreadPriority.Normal

    Thread3.Priority = Threading.ThreadPriority.Normal

    End If

    PrioritiesCheckbox.Enabled = False 'Can't use this anymore.

    'Start running the threads.

    'Notice here that thread1 will start just before thread2. And so on.

    Thread1.Start()

    Thread2.Start()

    Thread3.Start()

    End Sub

    Private Sub BtnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPause.Click

    BtnResume.Enabled = True 'Can now press resume.

    'Temporarily suspends the threads.

    Thread1.Suspend()

    Thread2.Suspend()

    Thread3.Suspend()

    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

    'If threads are suspended, resume them and then abort.

    If Thread1.ThreadState = Threading.ThreadState.Suspended Then

    Thread1.Resume()

    Thread2.Resume()

    Thread3.Resume()

    End If

    'Abort all threads.

    Thread1.Abort()

    Thread2.Abort()

    Thread3.Abort()

    End Sub

    Private Sub BtnResume_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnResume.Click

    BtnResume.Enabled = False 'Can't press it now.

    'Resume all threads.

    Thread1.Resume()

    Thread2.Resume()

    Thread3.Resume()

    End Sub

    End Class


    Ιωάννης Μανουσάκης
Προβολή Τροφοδοσίας RSS με μορφή XML
Με χρήση του Community Server (Commercial Edition), από την Telligent Systems